<?php

namespace app\api\controller;

use app\common\model\Project as ProjectModel;
use app\api\validate\Project as ProjectValidate;
use think\Db;

/**
 * 库存管理--商品分类控制器
 * @package app\index\controller
 */
class Project extends Auth
{
    public $noNeedLogin = '';

    /**
     * 设置项目--修改或者添加项目
     * project_id
     *name   sort  price   add_block_price  add_block_time
     * normal_rate  dot_block_rate  add_block_rate  call_block_rate
     * overtime_queue_rate  overtime_dot_rate  overtime_add_block_rate
     * receiver_queue_rate  receiver_dot_rate  receiver_add_rate
     * recommend_rate time down_block_time block_number is_free_order is_store_recommend
     *is_user_show description
     */
    public function edit()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new ProjectValidate();
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }

            if (!empty($data['id'])) {
                $project = Db::name('qt_project')
                    ->where(['store_id'=>$this->store_id,'name'=>$data['name']])
                    ->where('id','<>',$data['id'])
                    ->find();
            }else {
                $project = Db::name('qt_project')->where(['store_id'=>$this->store_id,'name'=>$data['name']])->find();
            }

            if (!empty($project)) {
                $this->result('项目名称已经存在', '', 204);
            }

            if (!empty($data['id'])) {
                $msg = ProjectModel::update($data);
            } else {
                $data['store_id'] = $this->store_id;
                $data['status'] = 1;
                $msg = ProjectModel::create($data);
            }

            $msg ? $this->result('操作成功', '', 200) : $this->result('修改分类信息与原信息相同', '', 204);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 设置项目--项目列表
     * token
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $limit = $this->request->post('limit',10);
            $list = ProjectModel::where(
                    [
                        'store_id' => $this->userInfo['store_id'],
                        'status' => 1
                    ])
                ->field('sort,id,name,price')
                ->order('sort asc')
                ->paginate($limit);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 设置项目--删除项目
     * token
     */
    public function remove()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data['project_id'])){
                $this->result('缺少必要参数', '', 204);
            }
            $result = ProjectModel::where('id',$data['project_id'])->update(['status'=>2]);
            $result ? $this->result('删除成功', '', 200) : $this->result('删除失败', '', 204);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    public function detail()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if (empty($post['project_id'])) {
                $this->result('项目ID不能为空', '', 204);
            }
            $data = ProjectModel::where('id', $post['project_id'])->find();
            $this->result('操作成功', $data, 200);
        } else {
            $this->result('请求错误', '', 204);
        }
    }

    /**
     * 获取项目分类
     */
    public function getProjectCate(){
        if($this->request->isPost()){
            $data = Db::name('qt_project_category')
                ->field('id,name')
                ->where(['store_id'=>$this->store_id])
                ->order('sort asc')
                ->select();
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);
    }

}
